package com.isoftstone.activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.InputFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.isoftstone.util.RUtil;

/**
 *
 * fangxiaoyu 
 */
public class DialogBase extends Dialog {

	public DialogBase(Context context) {
		super(context);
	}

	public DialogBase(Context context, int theme) {
		super(context, theme);
	}
	
	public static class Builder {
		private Context context; //
		private String title; //
		private String message; //
		private String confirm_btnText; //
		private String cancel_btnText; //
		private int minHeight;  //
		private int hint = -1;   //
		private String text="";   //
		private boolean isShowEditor;  //
		private EditText edittext;
		private View contentView; //
		private int MaxLength;
		private DialogInterface.OnClickListener confirm_btnClickListener; 
		private DialogInterface.OnClickListener cancel_btnClickListener;

		public Builder(Context context) {
			this.context = context;
		}
		public Builder setEditorParams(int minHeight,int hint)
		{
			this.minHeight=minHeight;
			this.hint=hint;
			return this;
		}
		public Builder setEditorParams(int minHeight,String text)
		{
			this.minHeight=minHeight;
			this.text=text;
			return this;
		}
        public String getEditorContent()
        {
        	if(edittext!=null)
        	{
        		return edittext.getText().toString();
        	}
        	else
        	{
        		return "";
        	}
        }
		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}
        public Builder setShowEditor(boolean isShowEditor)
        {
        	this.isShowEditor=isShowEditor;
        	return this;
        }
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setMaxLength(int MaxLength) {
			this.MaxLength = MaxLength;
			return this;
		}
		/**
		 * @param v View
		 * @return
		 */
		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		public Builder setPositiveButton(int confirm_btnText,
				DialogInterface.OnClickListener listener) {
			this.confirm_btnText = (String) context
					.getText(confirm_btnText);
			this.confirm_btnClickListener = listener;
			return this;
		}

		public Builder setPositiveButton(String confirm_btnText,
				DialogInterface.OnClickListener listener) {
			this.confirm_btnText = confirm_btnText;
			this.confirm_btnClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(int cancel_btnText,
				DialogInterface.OnClickListener listener) {
			this.cancel_btnText = (String) context
					.getText(cancel_btnText);
			this.cancel_btnClickListener = listener;
			return this;
		}

		public Builder setNegativeButton(String cancel_btnText,
				DialogInterface.OnClickListener listener) {
			this.cancel_btnText = cancel_btnText;
			this.cancel_btnClickListener = listener;
			return this;
		}

		public DialogBase create() {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// instantiate the dialog with the custom Theme
			final DialogBase dialog = new DialogBase(context, RUtil.instance.getStyle("plugin_dialogBaseStyle"));
			View layout = inflater.inflate(RUtil.instance.getLayout("plugin_dialog_base"), null);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			// set the dialog title
			((TextView) layout.findViewById(RUtil.instance.getId("title"))).setText(title);
			((TextView) layout.findViewById(RUtil.instance.getId("title"))).getPaint().setFakeBoldText(true);
			if(isShowEditor)
			{
				edittext=(EditText) layout.findViewById(RUtil.instance.getId("edittext"));
				edittext.setMinHeight(minHeight);
				if (hint != -1) {
					edittext.setHint(hint);
				}
				
				if(MaxLength > 0){
					edittext.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MaxLength)});
				}
				edittext.setText(text);
				edittext.setSelection(text.length());
				edittext.setVisibility(View.VISIBLE);
			}
			// set the confirm button
			if (confirm_btnText != null) {
				((Button) layout.findViewById(RUtil.instance.getId("confirm_btn")))
						.setText(confirm_btnText);
					((Button) layout.findViewById(RUtil.instance.getId("confirm_btn")))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									dialog.dismiss();
									if (confirm_btnClickListener != null) {
										confirm_btnClickListener.onClick(dialog,
												DialogInterface.BUTTON_POSITIVE);
									}
								}
							});
			} else {
				layout.findViewById(RUtil.instance.getId("confirm_btn")).setVisibility(
						View.GONE);
				layout.findViewById(RUtil.instance.getId("view_split")).setVisibility(View.GONE);
				layout.findViewById(RUtil.instance.getId("cancel_btn")).setBackgroundResource(RUtil.instance.getDrawable("plugin_dialog_single_btn_select"));
			}
			// set the cancel button
			if (cancel_btnText != null) {
				((Button) layout.findViewById(RUtil.instance.getId("cancel_btn")))
						.setText(cancel_btnText);
					((Button) layout.findViewById(RUtil.instance.getId("cancel_btn")))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									dialog.dismiss();
									if (cancel_btnClickListener != null) {
										cancel_btnClickListener.onClick(dialog,
												DialogInterface.BUTTON_NEGATIVE);
									}
								}
							});
			} else {
				layout.findViewById(RUtil.instance.getId("cancel_btn")).setVisibility(
						View.GONE);
				layout.findViewById(RUtil.instance.getId("view_split")).setVisibility(View.GONE);
				layout.findViewById(RUtil.instance.getId("confirm_btn")).setBackgroundResource(RUtil.instance.getDrawable("plugin_dialog_single_btn_select"));
			}
			// set the content message
			if (message != null) {
				((TextView) layout.findViewById(RUtil.instance.getId("message"))).setText(message);
			} else if (contentView != null) {
				/*((LinearLayout) layout.findViewById(R.id.message))
						.removeAllViews();
				((LinearLayout) layout.findViewById(R.id.message)).addView(
						contentView, new LayoutParams(
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT));*/
				
				((RelativeLayout)layout.findViewById(RUtil.instance.getId("content_view"))).removeAllViews();
				((RelativeLayout)layout.findViewById(RUtil.instance.getId("content_view"))).addView(contentView, new LayoutParams(
								LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT));
			}
			dialog.setContentView(layout);
			
			Window window = dialog.getWindow();
	        window.setWindowAnimations(RUtil.instance.getStyle("plugin_custom_dialog_anim"));
			
			return dialog;
		}
	}
}